<?php
namespace Admin\Controller;
use Think\Controller;

//文字栏目
class ColumnController extends Controller{
    public function index(){

    }

    //显示文字栏目
    public function listCha(){
        $ColumnModel = M('Column');
        $count = $ColumnModel->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        //$Page->setConfig('header', '共'.$count.'条记录');
        //进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $ColumnModel->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    //添加文件栏目 (页面)
    public function addCha(){
        $this->display();
    }

    //插入文字栏目 (动作)
    public function insertCha(){
        $ColumnModel = M('Column');
        $ColumnModel->name = I('post.name', '');
        $ColumnModel->style = '文字';
        $ColumnModel->content = I('post.content', '', false);
        $ColumnModel->sortnum = I('post.sortnum', 1, 'int');
        $ColumnModel->createtime = date('Y-m-d H:i:s');
        $ColumnModel->updatetime = date('Y-m-d H:i:s');
        $action_result = $ColumnModel->add();
        if($action_result){
            $this->success('添加栏目成功', 'addCha', 3);
        }else{
            $this->error('添加栏目失败', '', 3);
        }
    }

    //编辑文字栏目 (页面)
    public function editCha($tid=''){
        if(!$tid)exit('非法操作');
        $ColumnModel = M('Column');
        $data = $ColumnModel->find($tid);
        $this->assign('data', $data);
        $this->display();
    }

    //编辑文字栏目 （动作）
    public function updateCha(){
        $tid = I('post.tid');
        if(!$tid)exit('非法操作');
        $ColumnModel = M('Column');
        $ColumnModel->find($tid);
        $ColumnModel->name = I('post.name');
        $ColumnModel->content = I('post.content');
        $ColumnModel->sortnum = I('post.sortnum', 1, 'int');
        $action_result = $ColumnModel->save();
        if($action_result){
            $this->success('编辑栏目成功', 'listCha', 3);
        }else{
            $this->error('编辑栏目失败');
        }
    }

    //删除文字栏目
    public function deleteOneCha($tid=''){
        if(!$tid)exit('非法操作');
        $ColumnModel = M('Column');
        $action_result = $ColumnModel->delete($tid);
        if($action_result){
            $this->success('删除栏目成功', 'Column/listCha', 3);
        }else{
            $this->error('删除栏目失败', '', 3);
        }
    }

    //批量删除
    public function batDelChaCol(){
        $tids = I('post.tids');
        if(empty($tids))exit;
        $tids = implode(',', $tids);
        $ColumnModel = M('Column');
        $action_result = $ColumnModel->delete($tids);
        if($action_result){
            $this->success('批量删除栏目成功', 'Column/listCha', 3);
        }else{
            $this->error('批量删除栏目失败', '', 3);
        }
    }

}